package com.ruoyi.system.service.impl;

import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.cgRecordMapper;
import com.ruoyi.system.domain.cgRecord;
import com.ruoyi.system.service.IcgRecordService;

/**
 * 充电枪记录Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-06-30
 */
@Service
public class cgRecordServiceImpl implements IcgRecordService 
{
    @Autowired
    private cgRecordMapper cgRecordMapper;

    /**
     * 查询充电枪记录
     * 
     * @param recordId 充电枪记录主键
     * @return 充电枪记录
     */
    @Override
    public cgRecord selectcgRecordByRecordId(Integer recordId)
    {
        return cgRecordMapper.selectcgRecordByRecordId(recordId);
    }

    /**
     * 查询充电枪记录列表
     * 
     * @param cgRecord 充电枪记录
     * @return 充电枪记录
     */
    @Override
    public List<cgRecord> selectcgRecordList(cgRecord cgRecord)
    {
        return cgRecordMapper.selectcgRecordList(cgRecord);
    }

    /**
     * 新增充电枪记录
     * 
     * @param cgRecord 充电枪记录
     * @return 结果
     */
    @Override
    public int insertcgRecord(cgRecord cgRecord)
    {
        return cgRecordMapper.insertcgRecord(cgRecord);
    }

    /**
     * 修改充电枪记录
     * 
     * @param cgRecord 充电枪记录
     * @return 结果
     */
    @Override
    public int updatecgRecord(cgRecord cgRecord)
    {
        return cgRecordMapper.updatecgRecord(cgRecord);
    }

    /**
     * 批量删除充电枪记录
     * 
     * @param recordIds 需要删除的充电枪记录主键
     * @return 结果
     */
    @Override
    public int deletecgRecordByRecordIds(Integer[] recordIds)
    {
        return cgRecordMapper.deletecgRecordByRecordIds(recordIds);
    }

    /**
     * 删除充电枪记录信息
     * 
     * @param recordId 充电枪记录主键
     * @return 结果
     */
    @Override
    public int deletecgRecordByRecordId(Integer recordId)
    {
        return cgRecordMapper.deletecgRecordByRecordId(recordId);
    }

    @Override
    public List<Map<String, Object>> selectAllStationSimple() {
        return cgRecordMapper.selectAllStationSimple();
    }

    @Override
    public Map<String, Object> selectGunAnalysisByStationId(Integer stationId) {
        Map<String, Object> map = cgRecordMapper.selectGunAnalysisByStationId(stationId);
        Integer gunUseCount = map.get("gunUseCount") == null ? 0 : ((Number)map.get("gunUseCount")).intValue();
        Integer gunTotalCount = map.get("gunTotalCount") == null ? 0 : ((Number)map.get("gunTotalCount")).intValue();
        double useRate = 0;
        if (gunTotalCount > 0) {
            useRate = (double) gunUseCount / gunTotalCount * 100;
        }
        map.put("gunUseRate", useRate);
        Integer totalWorkDuration = map.get("totalWorkDuration") == null ? 0 : ((Number)map.get("totalWorkDuration")).intValue();
        map.put("totalWorkHour", totalWorkDuration != null ? Math.round(totalWorkDuration / 60.0 * 10) / 10.0 : 0);
        return map;
    }

    @Override
    public List<Map<String, Object>> selectDailyGunAnalysis(Integer stationId) {
        List<Map<String, Object>> list = cgRecordMapper.selectDailyGunAnalysis(stationId);
        int gunTotalCount = cgRecordMapper.selectGunTotalCountByStationId(stationId);
        for (Map<String, Object> day : list) {
            Integer gunUseCount = day.get("gunUseCount") == null ? 0 : ((Number)day.get("gunUseCount")).intValue();
            Integer totalWorkDuration = day.get("totalWorkDuration") == null ? 0 : ((Number)day.get("totalWorkDuration")).intValue();
            double useRate = 0;
            if (gunTotalCount > 0) {
                useRate = (double) gunUseCount / gunTotalCount * 100;
            }
            day.put("gunUseRate", useRate);
            day.put("totalWorkHour", totalWorkDuration != null ? Math.round(totalWorkDuration / 60.0 * 10) / 10.0 : 0);
        }
        return list;
    }

    @Override
public Map<String, Object> selectGunAnalysisByStationIdAndMonth(Integer stationId, String month) {
    Map<String, Object> map = cgRecordMapper.selectGunAnalysisByStationIdAndMonth(stationId, month);
    Integer gunUseCount = map.get("gunUseCount") == null ? 0 : ((Number)map.get("gunUseCount")).intValue();
    Integer gunTotalCount = map.get("gunTotalCount") == null ? 0 : ((Number)map.get("gunTotalCount")).intValue();
    double useRate = 0;
    if (gunTotalCount > 0) {
        useRate = (double) gunUseCount / gunTotalCount * 100;
    }
    map.put("gunUseRate", useRate);
    Integer totalWorkDuration = map.get("totalWorkDuration") == null ? 0 : ((Number)map.get("totalWorkDuration")).intValue();
    map.put("totalWorkHour", totalWorkDuration != null ? Math.round(totalWorkDuration / 60.0 * 10) / 10.0 : 0);
    return map;
}

@Override
public List<Map<String, Object>> selectDailyGunAnalysisByMonth(Integer stationId, String month) {
    List<Map<String, Object>> list = cgRecordMapper.selectDailyGunAnalysisByMonth(stationId, month);
    int gunTotalCount = cgRecordMapper.selectGunTotalCountByStationId(stationId);
    for (Map<String, Object> day : list) {
        Integer gunUseCount = day.get("gunUseCount") == null ? 0 : ((Number)day.get("gunUseCount")).intValue();
        Integer totalWorkDuration = day.get("totalWorkDuration") == null ? 0 : ((Number)day.get("totalWorkDuration")).intValue();
        double useRate = 0;
        if (gunTotalCount > 0) {
            useRate = (double) gunUseCount / gunTotalCount * 100;
        }
        day.put("gunUseRate", useRate);
        day.put("totalWorkHour", totalWorkDuration != null ? Math.round(totalWorkDuration / 60.0 * 10) / 10.0 : 0);
    }
    return list;
}
 
}
